#include <QDebug>
#include <QFile>
#include <QDateTime>

#include <QFileDialog>

#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

//    setWindowFlags(Qt::FramelessWindowHint|Qt::WindowTitleHint);

    initStyle();

    initForm();

    initData();

    initEvent();
}

MainWindow::~MainWindow()
{

    if(NULL != frm_battery_ptr)
        delete frm_battery_ptr;

    if(NULL != frm_bgdemo_ptr)
        delete frm_bgdemo_ptr;

    if(NULL != frm_btndefence_ptr)
        delete frm_btndefence_ptr;

    if(NULL != frm_color_ptr)
        delete frm_color_ptr;

    if(NULL != frm_comtool_ptr)
        delete frm_comtool_ptr;

    if(NULL != frm_countcode_ptr)
        delete frm_countcode_ptr;

    if(NULL != frm_dbpage_ptr)
        delete frm_dbpage_ptr;

    if(NULL != frm_devicesizetable_ptr)
        delete frm_devicesizetable_ptr;

    if(NULL != frm_emailtool_ptr)
        delete frm_emailtool_ptr;

    if(NULL != frm_flatui_ptr)
        delete frm_flatui_ptr;

    if(NULL != frm_frameless_ptr)
        delete frm_frameless_ptr;

    if(NULL != frm_gifwidget_ptr)
        delete frm_gifwidget_ptr;

    if(NULL != frm_ipaddress_ptr)
        delete frm_ipaddress_ptr;

    if(NULL != frm_lightbtn_ptr)
        delete frm_lightbtn_ptr;

    if(NULL != frm_lineeditnext_ptr)
        delete frm_lineeditnext_ptr;

    if(NULL != frm_lunnarcaledar_ptr)
        delete frm_lunnarcaledar_ptr;

    if(NULL != frm_mask_ptr)
        delete frm_mask_ptr;

    if(NULL != frm_movewidget_ptr)
        delete frm_movewidget_ptr;

    if(NULL != frm_navbtn_ptr)
        delete frm_navbtn_ptr;

    if(NULL != frm_nettool_ptr)
        delete frm_nettool_ptr;

    if(NULL != frm_ntpclient_ptr)
        delete frm_ntpclient_ptr;

    if(NULL != frm_pngtool_ptr)
        delete frm_pngtool_ptr;

    if(NULL != frm_qwtdemo_ptr)
        delete frm_qwtdemo_ptr;

    if(NULL != frm_savelog_ptr)
        delete frm_savelog_ptr;

    if(NULL != frm_saveruntime_ptr)
        delete frm_saveruntime_ptr;

    if(NULL != frm_screenwidget_ptr)
        delete frm_screenwidget_ptr;

    if(NULL != frm_styledemo_ptr)
        delete frm_styledemo_ptr;

    if(NULL != frm_videopanel_ptr)
        delete frm_videopanel_ptr;

    if(NULL != frm_videowidget_ptr)
        delete frm_videowidget_ptr;

    if(NULL != frm_zhtopy_ptr)
        delete frm_zhtopy_ptr;

    widgets.clear();

    delete ui;
}

void MainWindow::initForm()
{
    int wid = width();

//    minButton = new QToolButton(this);
//    closeButton = new QToolButton(this);
//    maxButton = new QToolButton(this);

//    QPixmap minPix = style()->standardPixmap(QStyle::SP_TitleBarMinButton);
//    QPixmap closePix = style()->standardPixmap(QStyle::SP_TitleBarCloseButton);
//    QPixmap maxPix = style()->standardPixmap(QStyle::SP_TitleBarMaxButton);

//    minButton->setIcon(minPix);
//    closeButton->setIcon(closePix);
//    maxButton->setIcon(maxPix);

//    minButton->setGeometry(wid-65, 5, 20, 20);
//    closeButton->setGeometry(wid-25, 5, 20, 20);
//    maxButton->setGeometry(wid-45, 5, 20, 20);

//    minButton->setToolTip(tr("minimize"));
//    maxButton->setToolTip(tr("close"));
//    maxButton->setToolTip(tr("maximize"));

//    minButton->setStyleSheet("background-color:transparent;border:0px;margin-top: 0px;");
//    closeButton->setStyleSheet("background-color:transparent;border:0px;margin-top: 0px;");
//    maxButton->setStyleSheet("background-color:transparent;border:0px;margin-top: 0px;");


//    ui->pushButton_data_monitor->setCheckable(true);
//    ui->pushButton_data_monitor->setAutoExclusive(true);
//    ui->pushButton_data_query->setCheckable(true);
//    ui->pushButton_data_query->setAutoExclusive(true);
//    ui->pushButton_device_monitor->setCheckable(true);
//    ui->pushButton_device_monitor->setAutoExclusive(true);
//    ui->pushButton_line_monitor->setCheckable(true);
//    ui->pushButton_line_monitor->setAutoExclusive(true);
//    ui->pushButton_map_monitor->setCheckable(true);
//    ui->pushButton_map_monitor->setAutoExclusive(true);
//    ui->pushButton_system_monitor->setCheckable(true);
//    ui->pushButton_system_monitor->setAutoExclusive(true);

//    connect(closeButton, SIGNAL(clicked()), this, SLOT(windowClosed()));
//    connect(minButton, SIGNAL(clicked()), this, SLOT(windowMinimize()));
//    connect(maxButton, SIGNAL(clicked()), this, SLOT(windowMaximize()));

    frm_battery_ptr = new frmBattery(this);
    widgets.append(frm_battery_ptr);

    frm_bgdemo_ptr = new Widget(this);
    widgets.append(frm_bgdemo_ptr);

    frm_btndefence_ptr = new frmButtonDefence(this);
    widgets.append(frm_btndefence_ptr);

    frm_color_ptr = new ColorWidget(this);
    widgets.append(frm_color_ptr);

    frm_comtool_ptr = new frmComTool(this);
    widgets.append(frm_comtool_ptr);

    frm_countcode_ptr = new frmCountCode(this);
    widgets.append(frm_countcode_ptr);

    frm_dbpage_ptr = new frmDbPage(this);
    widgets.append(frm_dbpage_ptr);

    frm_devicesizetable_ptr = new frmDeviceSizeTable(this);
    widgets.append(frm_devicesizetable_ptr);

    frm_emailtool_ptr = new frmEmailTool(this);
    widgets.append(frm_emailtool_ptr);

    frm_flatui_ptr = new frmFlatUI(this);
    widgets.append(frm_flatui_ptr);

////    frm_frameless_ptr = new FramelessWidget(this);
////    widgets.append(frm_frameless_ptr);

    frm_gifwidget_ptr = new GifWidget(this);
    widgets.append(frm_gifwidget_ptr);

    frm_ipaddress_ptr = new frmIPAddress(this);
    widgets.append(frm_ipaddress_ptr);

    frm_lightbtn_ptr = new frmLightButton(this);
    widgets.append(frm_lightbtn_ptr);

    frm_lineeditnext_ptr = new frmLineEditNext(this);
    widgets.append(frm_lineeditnext_ptr);

    frm_lunnarcaledar_ptr = new frmLunarCalendarWidget(this);
    widgets.append(frm_lunnarcaledar_ptr);

    frm_mask_ptr = new frmMaskWidget(this);
    widgets.append(frm_mask_ptr);

    frm_mouseline_ptr = new frmMouseLine(this);
    widgets.append(frm_mouseline_ptr);

    frm_movewidget_ptr = new frmMoveWidget(this);
    widgets.append(frm_movewidget_ptr);

    frm_navbtn_ptr = new frmNavButton(this);
    widgets.append(frm_navbtn_ptr);

    frm_nettool_ptr = new frmNetTool(this);
    widgets.append(frm_nettool_ptr);

    frm_ntpclient_ptr = new frmNtpClient(this);
    widgets.append(frm_ntpclient_ptr);

    frm_pngtool_ptr = new frmPngTool(this);
    widgets.append(frm_pngtool_ptr);

    frm_qwtdemo_ptr = new frmQwtDemo(this);
    widgets.append(frm_qwtdemo_ptr);

    frm_savelog_ptr = new frmSaveLog(this);
    widgets.append(frm_savelog_ptr);

    frm_saveruntime_ptr = new frmSaveRunTime(this);
    widgets.append(frm_saveruntime_ptr);

    frm_screenwidget_ptr = new ScreenWidget(this);
    widgets.append(frm_screenwidget_ptr);

    frm_styledemo_ptr = new frmStyleDemo(this);
    widgets.append(frm_styledemo_ptr);

    frm_videopanel_ptr = new frmVideoPanel(this);
    widgets.append(frm_videopanel_ptr);

    frm_videowidget_ptr = new frmVideoWidget(this);
    widgets.append(frm_videowidget_ptr);

    frm_zhtopy_ptr = new frmZhToPY(this);
    widgets.append(frm_zhtopy_ptr);


    foreach (QWidget *w, widgets) {
        ui->stackedWidget->addWidget(w);
    }

    ui->stackedWidget->setCurrentIndex(26);
}

void MainWindow::initStyle()
{
    //加载样式表
//    QFile file(":/resources/qss/psblack.css");
//    QFile file(":/resources/qss/flatwhite.css");
    QFile file(":/resources/qss/lightblue.css");
//    QFile file(":/resources/qss/orangeyellow.css");
    if (file.open(QFile::ReadOnly)) {
        QString qss = QLatin1String(file.readAll());
        QString paletteColor = qss.mid(20, 7);
        qApp->setPalette(QPalette(QColor(paletteColor)));
        qApp->setStyleSheet(qss);
        file.close();
    }
}

void MainWindow::initData()
{
    ui->listView->setItems("1,0,0,,控件,,NULL");
    ui->listView->setItems("2,0,0,0xf03e,电池控件,,控件");
    ui->listView->setItems("2,0,0,0xf03d,IP地址,,控件");
    ui->listView->setItems("2,0,0,0xf108,背光按钮,,控件");
    ui->listView->setItems("2,0,0,0xf108,文本编辑器,,控件");
    ui->listView->setItems("2,0,0,0xf108,日历控件,,控件");
    ui->listView->setItems("2,0,0,0xf108,老鼠控件,,控件");
    ui->listView->setItems("2,0,0,0xf108,导航按钮,,控件");
    ui->listView->setItems("2,0,1,0xf108,曲线画,,控件");

    ui->listView->setItems("1,0,0,,样式,,NULL");
    ui->listView->setItems("2,0,0,0xf2bb,背景颜色,,样式");
    ui->listView->setItems("2,0,0,0xf2bb,数据库样式,,样式");
    ui->listView->setItems("2,0,0,0xf0f9,扁平样式,,样式");
    ui->listView->setItems("2,0,0,0xf242,屏蔽样式,,样式");
    ui->listView->setItems("2,0,0,0xf242,可移动窗口,,样式");
    ui->listView->setItems("2,0,1,0xf242,多样式,,样式");

    ui->listView->setItems("1,0,0,,系统,,NULL");
    ui->listView->setItems("2,0,0,0xf271,监控系统,,系统");
    ui->listView->setItems("2,0,0,0xf140,拾色器,,系统");
    ui->listView->setItems("2,0,0,0xf0b1,串口助手,,系统");
    ui->listView->setItems("2,0,0,0xf207,代码量统计工具,,系统");
    ui->listView->setItems("2,0,0,0xf207,磁盘空间,,系统");
    ui->listView->setItems("2,0,0,0xf207,邮件工具,,系统");
    ui->listView->setItems("2,0,0,0xf207,未知,,系统");
    ui->listView->setItems("2,0,0,0xf207,gif截图,,系统");
    ui->listView->setItems("2,0,0,0xf207,网络助手,,系统");
    ui->listView->setItems("2,0,0,0xf207,NTP服务器,,系统");
    ui->listView->setItems("2,0,0,0xf207,PNG查看器,,系统");
    ui->listView->setItems("2,0,0,0xf207,曲线画,,系统");
    ui->listView->setItems("2,0,0,0xf207,保持日志,,系统");
    ui->listView->setItems("2,0,0,0xf207,运行时间,,系统");
    ui->listView->setItems("2,0,0,0xf207,屏幕显示,,系统");
    ui->listView->setItems("2,0,0,0xf207,视频监控,,系统");
    ui->listView->setItems("2,0,0,0xf207,视频面板,,系统");
    ui->listView->setItems("2,0,1,0xf207,输入法,,系统");

    ui->listView->setItems("1,0,1,,帮助文档,,NULL");
    ui->listView->setItems("2,0,0,0xf0c2,系统帮助,,帮助文档");
    ui->listView->setItems("2,0,0,0xf075,关于系统,,帮助文档");
    ui->listView->setItems("2,0,1,0xf015,关于系统,,帮助文档");
}

void MainWindow::initEvent()
{
//    connect(closeButton, SIGNAL(clicked()), this, SLOT(windowClosed()));
        QObject::connect(ui->listView, SIGNAL(pressed(const QString &, const QString &)), this, SLOT(slot_listview_pressed(const QString &, const QString &)));
}

void MainWindow::paintEvent(QPaintEvent *)
{
    int wid = width();
//    minButton->setGeometry(wid-65, 5, 20, 20);
//    closeButton->setGeometry(wid-25, 5, 20, 20);
//    maxButton->setGeometry(wid-45, 5, 20, 20);
}

void MainWindow::windowClosed()
{
    this->close();
}

void MainWindow::windowMaximize()
{
    this->showMaximized();
}

void MainWindow::windowMinimize()
{
    this->showMinimized();
}
/*

void MainWindow::on_pushButton_device_monitor_clicked()
{


    ui->pushButton_data_query->setChecked(false);
    ui->pushButton_data_monitor->setChecked(false);
    ui->pushButton_device_monitor->setChecked(true);
    ui->pushButton_line_monitor->setChecked(false);
    ui->pushButton_map_monitor->setChecked(false);
    ui->pushButton_system_monitor->setChecked(false);

    ui->stackedWidget->setCurrentIndex(0);
}

void MainWindow::on_pushButton_map_monitor_clicked()
{
    ui->pushButton_data_query->setChecked(false);
    ui->pushButton_data_monitor->setChecked(false);
    ui->pushButton_device_monitor->setChecked(false);
    ui->pushButton_line_monitor->setChecked(false);
    ui->pushButton_map_monitor->setChecked(true);
    ui->pushButton_system_monitor->setChecked(false);

    ui->stackedWidget->setCurrentIndex(1);
}

void MainWindow::on_pushButton_data_monitor_clicked()
{
    ui->pushButton_data_query->setChecked(false);
    ui->pushButton_data_monitor->setChecked(true);
    ui->pushButton_device_monitor->setChecked(false);
    ui->pushButton_line_monitor->setChecked(false);
    ui->pushButton_map_monitor->setChecked(false);
    ui->pushButton_system_monitor->setChecked(false);


}

void MainWindow::on_pushButton_line_monitor_clicked()
{
    ui->pushButton_data_query->setChecked(false);
    ui->pushButton_data_monitor->setChecked(false);
    ui->pushButton_device_monitor->setChecked(false);
    ui->pushButton_line_monitor->setChecked(true);
    ui->pushButton_map_monitor->setChecked(false);
    ui->pushButton_system_monitor->setChecked(false);
}

void MainWindow::on_pushButton_data_query_clicked()
{
    ui->pushButton_data_query->setChecked(true);
    ui->pushButton_data_monitor->setChecked(false);
    ui->pushButton_device_monitor->setChecked(false);
    ui->pushButton_line_monitor->setChecked(false);
    ui->pushButton_map_monitor->setChecked(false);
    ui->pushButton_system_monitor->setChecked(false);
}

void MainWindow::on_pushButton_system_monitor_clicked()
{
    ui->pushButton_data_query->setChecked(false);
    ui->pushButton_data_monitor->setChecked(false);
    ui->pushButton_device_monitor->setChecked(false);
    ui->pushButton_line_monitor->setChecked(false);
    ui->pushButton_map_monitor->setChecked(false);
    ui->pushButton_system_monitor->setChecked(true);
}
*/

void MainWindow::slot_listview_pressed(QString text, QString parenttext)
{
    if (parenttext == "控件")
    {
        if (text == "电池控件")
        {
            ui->stackedWidget->setCurrentIndex(0);
        } else if (text == "IP地址") {
            ui->stackedWidget->setCurrentIndex(11);
        } else if (text == "背光按钮") {
            ui->stackedWidget->setCurrentIndex(12);
        } else if (text == "文本编辑器") {
            ui->stackedWidget->setCurrentIndex(13);
        } else if (text == "日历控件") {
            ui->stackedWidget->setCurrentIndex(14);
        } else if (text == "老鼠控件") {
            ui->stackedWidget->setCurrentIndex(16);
        } else if (text == "导航按钮") {
            ui->stackedWidget->setCurrentIndex(18);
        } else if (text == "曲线画") {
            ui->stackedWidget->setCurrentIndex(22);
        }
    } else if (parenttext == "样式") {
        if (text == "背景颜色")
        {
            ui->stackedWidget->setCurrentIndex(1);
        } else if (text == "数据库样式") {
            ui->stackedWidget->setCurrentIndex(6);
        } else if (text == "扁平样式") {
            ui->stackedWidget->setCurrentIndex(9);
        } else if (text == "屏蔽样式") {
            ui->stackedWidget->setCurrentIndex(15);
        } else if (text == "可移动窗口") {
            ui->stackedWidget->setCurrentIndex(17);
        } else if (text =="多样式") {
            ui->stackedWidget->setCurrentIndex(26);
        }
    } else if (parenttext == "系统") {
        if (text == "监控系统")
        {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "拾色器") {
            ui->stackedWidget->setCurrentIndex(3);
        } else if (text == "串口助手") {
            ui->stackedWidget->setCurrentIndex(4);
        } else if (text == "代码量统计工具") {
            ui->stackedWidget->setCurrentIndex(5);
        } else if (text == "磁盘空间") {
            ui->stackedWidget->setCurrentIndex(7);
        } else if (text == "邮件工具") {
            ui->stackedWidget->setCurrentIndex(8);
        } else if (text == "gif截图") {
            ui->stackedWidget->setCurrentIndex(10);
        } else if (text == "网络助手") {
            ui->stackedWidget->setCurrentIndex(19);
        } else if (text == "NTP服务器") {
            ui->stackedWidget->setCurrentIndex(20);
        } else if (text == "PNG查看器") {
            ui->stackedWidget->setCurrentIndex(21);
        } else if (text == "曲线画") {
            ui->stackedWidget->setCurrentIndex(22);
        } else if (text == "保持日志") {
            ui->stackedWidget->setCurrentIndex(23);
        }  else if (text == "运行时间") {
            ui->stackedWidget->setCurrentIndex(24);
        }  else if (text == "屏幕显示") {
            ui->stackedWidget->setCurrentIndex(25);
        } else if (text == "视频监控") {
            ui->stackedWidget->setCurrentIndex(27);
        } else if (text == "视频面板") {
            ui->stackedWidget->setCurrentIndex(28);
        } else if(text == "输入法") {
            ui->stackedWidget->setCurrentIndex(29);
        }

    } else {
        if (text == "电池控件")
        {
            ui->stackedWidget->setCurrentIndex(0);
        } else if (text == "背景颜色") {
            ui->stackedWidget->setCurrentIndex(1);
        } else if (text == "设备") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "监控系统") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else if (text == "") {
            ui->stackedWidget->setCurrentIndex(2);
        } else {
            ui->stackedWidget->setCurrentIndex(2);
        }
    }

}
